package com.winxuan.channel.controller.member;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.winxuan.channel.model.member.Resource;
import com.winxuan.channel.model.member.User;
import com.winxuan.channel.model.object.ChannelResult;
import com.winxuan.channel.service.member.ResourceService;
import com.winxuan.channel.service.member.UserService;
import com.winxuan.channel.support.interceptor.MemberAware;
import com.winxuan.channel.support.interceptor.security.MemberAuthenticationManager;

@Controller
@RequestMapping(value="/member")
public class MemberController implements MemberAware{

	private Logger log = LoggerFactory.getLogger(Thread.class);
	@Autowired
	private UserService userService;
	@Autowired
	private ResourceService resourceService;
	@Autowired
	private MemberAuthenticationManager memberAuthenticationManager;

	private User user;

	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView def(){
		System.out.println("Hello World");
		return new ModelAndView("/result");
	}

	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public ModelAndView get(@PathVariable("id")Integer id) throws Exception{
		ModelAndView modelAndView = null;
		User user = userService.get(id);
		if(user!=null){
			modelAndView = new ModelAndView("/result");
			modelAndView.addObject("user",user);
		}else{
			throw new Exception("用户不存在!");
		}
		return modelAndView;
	}

	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String login(){
		return "member/login";
	}

	@RequestMapping(value="/login",method=RequestMethod.POST)
	public ChannelResult doLogin(@RequestParam("username")String userName,@RequestParam("password")String passWord,HttpServletRequest request) throws Exception{
		ChannelResult channelResult = new ChannelResult();
		channelResult.setSuccess(false);
		User user = userService.findUserByUserName(userName);
		if(user!=null){
			if(passWord.equals(user.getPassword())){
				memberAuthenticationManager.cacheMemberPermission(user, request);
				channelResult.setSuccess(true);
			}else{
				channelResult.setMsg("密码错误");
			}
		}else{
			channelResult.setMsg("用户不存在!");
		}
		return channelResult;
	}

	@RequestMapping(value="/logout",method=RequestMethod.POST)
	public ChannelResult logout(HttpServletRequest request){
		ChannelResult channelResult = new ChannelResult();
		memberAuthenticationManager.releaseMemberPermission(request);
		channelResult.setSuccess(true);
		return channelResult;
	}


	@RequestMapping(value="/list",method=RequestMethod.GET)
	public ModelAndView list(){
		log.info("Hello World");
		List<Resource> list = resourceService.loadAllResources();
		for(Resource r : list){
			log.info(r.getName() + " - " + r.getUrl());
		}
		System.out.println("Current:"+user.getUsername());
		return new ModelAndView("/result");
	}

	@Override
	public void setCustomer(User user) {
		this.user = user;
	}


}
